package com.stoneapple.timesheet.Servlet;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.stoneapple.timesheet.Constrain;
import com.stoneapple.timesheet.dao.TSDao;


import java.util.logging.Logger;

@SuppressWarnings("serial")
public class CreateTimesheetServlet extends HttpServlet{
	private static final Logger log=Logger.getLogger(CreateTimesheetServlet.class.getName());
	
	public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException{
		
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
		if (user == null) {
			String loginUrl = userService.createLoginURL(req.getRequestURI());
			resp.sendRedirect(loginUrl);

		} else {
			log.info("User Id: " + user);
			
			String projectcode = Constrain.checkNullString(req.getParameter("projectcode"));
			String assignmentname = Constrain.checkNullString(req.getParameter("assignmentname"));
			String ihours=Constrain.checkNullString(req.getParameter("hours"));
		
			
			// change "ihours" from string to int
			
				
				int hours=Integer.parseInt(ihours);
				
			
			

			log.info("Timesheet information:");
			log.info("Project Code: " + projectcode + ", Assignment Name: " + assignmentname +",Hours: "+hours );

		TSDao tsdao=TSDao.getInstance();
		tsdao.save(user.getUserId(), projectcode, assignmentname, hours);
		
			resp.sendRedirect("/timesheet");			
		}
	}
}
	
	


